package com.hdoit.ares.logical.acti.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.hdoit.ares.logical.acti.dao.ITaskGoldenBeanDao;
import com.hdoit.ares.logical.acti.model.TaskGoldenBean;
import com.hdoit.ares.logical.acti.service.ITaskGoldenBeanService;
import com.hdoit.ares.logical.cust.model.Customer;
import com.tennetcn.common.dao.base.impl.SuperService;
import com.tennetcn.common.enums.ModelStatus;
import com.tennetcn.common.utils.CommonUtils;
/**
 * @author      caochengmeng
 * @email       
 * @createtime  2018年03月29日 15:33:14
 * @comment
 */
@Component
public class TaskGoldenBeanServiceImpl extends SuperService<TaskGoldenBean> implements ITaskGoldenBeanService{

	private ITaskGoldenBeanDao taskGoldenBeanDao;
	
	public ITaskGoldenBeanDao getTaskGoldenBeanDao() {
		return taskGoldenBeanDao;
	}
	@Resource
	public void setTaskGoldenBeanDao(ITaskGoldenBeanDao taskGoldenBeanDao) {
		this.taskGoldenBeanDao = taskGoldenBeanDao;
	}
	
	private ITaskGoldenBeanService taskGoldenBeanService;
	public ITaskGoldenBeanService getTaskGoldenBeanService() {
		return taskGoldenBeanService;
	}
	@Resource
	public void setTaskGoldenBeanService(ITaskGoldenBeanService taskGoldenBeanService) {
		this.taskGoldenBeanService = taskGoldenBeanService;
	}
	@Override
	public TaskGoldenBean queryModelByCustomerId(String customerId) {
		return taskGoldenBeanDao.queryModelByCustomerId(customerId);
	}
	@Override
	public boolean setSort(List<TaskGoldenBean> taskGoldenBeanList){
		for(int i =0;i<=taskGoldenBeanList.size();i++){	
			taskGoldenBeanList.get(i).setSort(i+1);
		}
		if(applyChangeSorts(taskGoldenBeanList)){
			return true;
		}
		return false;
	}

	@Override
	public List<TaskGoldenBean> sortForArticalClassify(List<TaskGoldenBean> taskGoldenBeanList) {
		if (taskGoldenBeanList != null && taskGoldenBeanList.size() >= 2) {
			Collections.sort(taskGoldenBeanList, new Comparator<TaskGoldenBean>() {
				@Override
				public int compare(TaskGoldenBean o1, TaskGoldenBean o2) {
					return (int) (o2.getGoldenBean() - o1.getGoldenBean());
				}
			});
			// //与上面等价，lambda表达式处理
			// Collections.sort(accountInfoList,
			// Comparator.comparing(TaskGoldenBean::getGoldenBean));
		}
		return taskGoldenBeanList;
	}
	@Override
	public boolean applyChangeSorts(List<TaskGoldenBean> taskGoldenBeans) {
		if(taskGoldenBeans==null||taskGoldenBeans.size()==0){
			return false;
		}
		boolean result=false;
		for (TaskGoldenBean taskGoldenBean : taskGoldenBeans) {
			if(ModelStatus.update.equals(taskGoldenBean.getModelStatus())){
				result=updateModelSelective(taskGoldenBean);
			}
		}
		return result;
	}
	
	@Override
	public boolean addTaskGoldenBean(Customer customer) {
		TaskGoldenBean taskGoldenBean = new TaskGoldenBean();
		taskGoldenBean.setId(CommonUtils.getKeyId());
		taskGoldenBean.setCustomerId(customer.getId());
		taskGoldenBean.setGoldenBean(0);
		taskGoldenBean.setSort(0);
		taskGoldenBean.setModelStatus(ModelStatus.add);
		return applyChange(taskGoldenBean);
	}
	

}